Skip to content

Comments

Add Perceval Parameter Expression handling#13

Open
anthonyrtw wants to merge 2 commits intomerlinquantum:release-0.3-OLDfrom
anthonyrtw:parameter-expressions
Open

Add Perceval Parameter Expression handling#13
anthonyrtw wants to merge 2 commits intomerlinquantum:release-0.3-OLDfrom
anthonyrtw:parameter-expressions

Conversation

@anthonyrtw
Copy link
Contributor

@anthonyrtw anthonyrtw commented Jun 24, 2025

Summary

  • Modify CircuitConverter to convert circuit components with Perceval parameter expressions, Expression.

Context

  • Perceval components may contain parameter expressions, Expression. These are objects which represent arithmetic manipulations of the Parameter class.
  • Currently, when converting circuits with Expression, MerLin ignores the Expression and does not raise any error - leading the user to think that their circuit may have been successfully converted into a tensor.

Proposed Changes

  • Add hidden method, _parse_expression to CircuitConverter, which evaluates a Perceval Expression by parsing its algebraic representation using the ast library and substituting in the values of its parent parameters.
from perceval import Circuit, PS, P
from merlin import CircuitConverter
import torch
import numpy as np

# Consider a single phase shifter
circuit = Circuit(2) // PS(2 * P("A"))

# Let's print the unitary for phase of pi and a phase of 2pi
phases = np.pi * torch.tensor([[0.5], [1]])

circuit_graph = CircuitConverter(circuit, ["A"])
print(circuit_graph.to_tensor(phases).real)
tensor([[[-1.,  0.],
         [ 0.,  1.]],

        [[ 1.,  0.],
         [ 0.,  1.]]])

@anthonyrtw
Copy link
Contributor Author

The anticipated changes from Perceval for this PR will be available in Perceval 1.0.0.

@anthonyrtw anthonyrtw marked this pull request as ready for review September 22, 2025 14:33
@anthonyrtw anthonyrtw changed the base branch from main to release-0.2 October 10, 2025 07:38
@QuandelaOSS QuandelaOSS changed the base branch from release-0.2 to release-0.2.1 November 3, 2025 17:29
@ben9871 ben9871 changed the base branch from release-0.2.1 to release-0.3 November 10, 2025 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant